package com.ask.server.service.impl;

import com.ask.common.constant.StatusConstant;
import com.ask.common.exception.BaseException;
import com.ask.common.result.Result;
import com.ask.pojo.dto.CategoryDTO;
import com.ask.pojo.dto.CategoryQueryDTO;
import com.ask.pojo.entity.Category;
import com.ask.server.handler.GlobalExceptionHandler;
import com.ask.server.mapper.CategoryMapper;
import com.ask.server.service.ICategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    private final GlobalExceptionHandler globalExceptionHandler;

    public CategoryServiceImpl(GlobalExceptionHandler globalExceptionHandler) {
        this.globalExceptionHandler = globalExceptionHandler;
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // 默认为禁用状态
        category.setStatus(StatusConstant.DISABLE);
        // todo 其他信息添加
        category.setCreateTime(LocalDateTime.now());
        save(category);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    public Result deleteById(Long id) {
        Category category = getById(id);
        if (category == null)
            return Result.fail("种类不存在！");
        if (category.getStatus().equals(StatusConstant.ENABLE)) {
            return Result.fail("禁止删除启用中的种类！");
        }
        // todo 检查分类是否与某个资源关联，如有关联则禁止删除
        if (removeById(id)) {
            return Result.success("删除成功！");
        }
        return Result.fail("删除失败！");
    }

    /**
     * 跟新分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // todo update信息修改
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(1L);
        updateById(category);
    }

    /**
     * 启用/禁用分类
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(1L)
                .build();
        updateById(category);
    }

    /**
     * 根据类型查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> listByType(Integer type) {
        List<Category> list = query().eq("type", type).list();
        if (list == null || list.isEmpty())
            throw new BaseException("不存在该类型的分类！");
        return list;
    }

    /**
     * 分页查询
     * @param categoryQueryDTO
     * @return
     */
    @Override
    public List<Category> pageQuery(CategoryQueryDTO categoryQueryDTO) {
        Page<Category> page = query().like("name", categoryQueryDTO.getName())
                .eq("type", categoryQueryDTO.getType())
                .page(new Page<>(categoryQueryDTO.getPage(), categoryQueryDTO.getPageSize()));
        return page.getRecords();
    }
}